package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.DxmChukuDetail;
import com.ruoyi.system.mapper.DxmChukuMapper;
import com.ruoyi.system.domain.DxmChuku;
import com.ruoyi.system.service.IDxmChukuService;

/**
 * 出库管理Service业务层处理
 * 
 * @author 丁宪明
 * @date 2024-02-26
 */
@Service
public class DxmChukuServiceImpl implements IDxmChukuService 
{
    @Autowired
    private DxmChukuMapper dxmChukuMapper;

    /**
     * 查询出库管理
     * 
     * @param id 出库管理主键
     * @return 出库管理
     */
    @Override
    public DxmChuku selectDxmChukuById(Long id)
    {
        return dxmChukuMapper.selectDxmChukuById(id);
    }

    /**
     * 查询出库管理列表
     * 
     * @param dxmChuku 出库管理
     * @return 出库管理
     */
    @Override
    public List<DxmChuku> selectDxmChukuList(DxmChuku dxmChuku)
    {
        return dxmChukuMapper.selectDxmChukuList(dxmChuku);
    }

    /**
     * 新增出库管理
     * 
     * @param dxmChuku 出库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDxmChuku(DxmChuku dxmChuku)
    {
        dxmChuku.setCreateTime(DateUtils.getNowDate());
        int rows = dxmChukuMapper.insertDxmChuku(dxmChuku);
        insertDxmChukuDetail(dxmChuku);
        return rows;
    }

    /**
     * 修改出库管理
     * 
     * @param dxmChuku 出库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDxmChuku(DxmChuku dxmChuku)
    {
        dxmChukuMapper.deleteDxmChukuDetailByChukuid(dxmChuku.getId());
        insertDxmChukuDetail(dxmChuku);
        return dxmChukuMapper.updateDxmChuku(dxmChuku);
    }

    /**
     * 批量删除出库管理
     * 
     * @param ids 需要删除的出库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDxmChukuByIds(Long[] ids)
    {
        dxmChukuMapper.deleteDxmChukuDetailByChukuids(ids);
        return dxmChukuMapper.deleteDxmChukuByIds(ids);
    }

    /**
     * 删除出库管理信息
     * 
     * @param id 出库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDxmChukuById(Long id)
    {
        dxmChukuMapper.deleteDxmChukuDetailByChukuid(id);
        return dxmChukuMapper.deleteDxmChukuById(id);
    }

    @Override
    public List<Map<String, Object>> selectMoneyCount(DxmChuku dxmChuku) {
        return dxmChukuMapper.selectMoneyCount(dxmChuku);
    }

    @Override
    public List<Map<String, Object>> selectPayStatus(DxmChuku dxmChuku) {
        return dxmChukuMapper.selectPayStatus(dxmChuku);
    }

    @Override
    public List<Map<String, Object>> selectGoodsNumCount(DxmChuku dxmChuku) {
        return dxmChukuMapper.selectGoodsNumCount(dxmChuku);
    }

    @Override
    public List<Map<String, Object>> selectGoodsMoneyCount(DxmChuku dxmChuku) {
        return dxmChukuMapper.selectGoodsMoneyCount(dxmChuku);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param dxmChuku 出库管理对象
     */
    public void insertDxmChukuDetail(DxmChuku dxmChuku)
    {
        List<DxmChukuDetail> dxmChukuDetailList = dxmChuku.getDxmChukuDetailList();
        Long id = dxmChuku.getId();
        if (StringUtils.isNotNull(dxmChukuDetailList))
        {
            List<DxmChukuDetail> list = new ArrayList<DxmChukuDetail>();
            for (DxmChukuDetail dxmChukuDetail : dxmChukuDetailList)
            {
                dxmChukuDetail.setChukuid(id);
                list.add(dxmChukuDetail);
            }
            if (list.size() > 0)
            {
                dxmChukuMapper.batchDxmChukuDetail(list);
            }
        }
    }
}
